<template>
	<view>
		<!-- 搜索框 -->
		<search-bar placeholder="请输入您想要的商品" cancelButton="none" @onSearchBarClicked="onSearchBarClicked"></search-bar>
		<view class="cate">
			<!-- 左侧区域 -->
			<view class="left-scroll-view">
				<scroll-view scroll-y :style="{height: contentHeight + 'px'}" scroll-with-animation>
					<block v-for="(cate,index) in cateList" :key="item.cat_id">
						<view :class="['left-tab',index === activeIndex ? 'active-tab' : '']"
							@click="onTabClicked(index)">
							{{cate.cat_name}}
						</view>
					</block>
				</scroll-view>
			</view>
			<!-- 右侧区域 -->
			<view class="right-scroll-view">
				<scroll-view scroll-y="true" :style="{height: contentHeight + 'px'}" scroll-with-animation>
					<view class="levelII-item" v-for="levelII in cateLevelIIList" :key="levelII.cat_id">
						<view class="levelII-title">
							{{ levelII.cat_name }}
						</view>
						<view class="level-show">
							<block v-for="lv3 in levelII.children" :key="lv3.cat_id">
								<view class="levelIII-cover" @click="onGoodsItemClicked(lv3)">
									<image :src="lv3.cat_icon"></image>
									<text>{{ lv3.cat_name }}</text>
								</view>
							</block>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCateList
	} from '../../service/cate_api.js'

	import SearchBar from '../../components/SearchBar/SearchBar.vue'

	import BadgeMixin from '../../mixins/badge-mixin.js'


	export default {
		mixins: [BadgeMixin],
		data() {
			return {
				contentHeight: uni.$contentHeight,
				activeIndex: 0,
				cateList: [],
				cateLevelIIList: []
			}
		},
		components: {
			SearchBar
		},
		methods: {
			onTabClicked(index) {
				this.activeIndex = index
				this.cateLevelIIList = this.cateList[index].children
			},
			onGoodsItemClicked(item) {
				uni.navigateTo({
					url: `/subpkg/goods-list/goods-list?cid=${item.cat_id}`
				})
			},
			onSearchBarClicked() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},
		onLoad() {
			getCateList().then(res => {
				if (res.meta.status === 200) {
					this.cateList = res.message
					this.cateLevelIIList = res.message[0].children
				} else {
					uni.$showMsg()
				}
			})
		}
	}
</script>

<style lang="scss">
	.cate {
		display: flex;

		.left-scroll-view {
			width: 176rpx;
			background-color: #f8f8f8;
			text-align: center;

			.left-tab {
				width: 100%;
				height: 108rpx;
				line-height: 108rpx;
				font-size: 26rpx;
			}

			.active-tab {
				position: relative;
				color: #e93b3d;
				background-color: #fff;

				&::before {
					position: absolute;
					content: '';
					display: block;
					width: 6rpx;
					height: 60rpx;
					background-color: red;
					top: 50%;
					left: 4rpx;
					transform: translateY(-50%)
				}
			}
		}

		.right-scroll-view {
			flex: 1;

			.levelII-item {
				.levelII-title {
					font-size: 32rpx;
					font-weight: bold;
					text-align: center;
					padding: 36rpx 0 0;
				}

				.level-show {
					display: flex;
					flex-wrap: wrap;

					.levelIII-cover {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 33.3%;
						font-size: 24rpx;

						image {
							width: 154rpx;
							height: 160rpx;
						}

					}

				}
			}
		}
	}
</style>
